package com.gsit.expenditure.controller;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.util.Callback;

import com.gsit.expenditure.client.domina.VehicleBean;
import com.gsit.expenditure.component.CheckBoxTableCell;
import com.gsit.expenditure.domain.Vehicle;
import com.gsit.expenditure.services.VehicleServices;
import com.gsit.expenditure.utils.AppContextLoader;

public class VehicleListController extends BaseController implements Initializable {

	@FXML 
	private TableView<Vehicle> vehicleListTable;
	
	@FXML
	private StackPane centerPane;
	
	@FXML
	private TableColumn<VehicleBean, String> vehicleNumberColumn;
	@FXML
	private TableColumn<VehicleBean, String> vehicleNameColumn;
	@FXML
	private TableColumn<VehicleBean, String> typeColumn;
	@FXML
	private TableColumn<VehicleBean, String> modelColum;
	@FXML
	private TableColumn<VehicleBean, String> engnoColumn;
	@FXML
	private TableColumn<VehicleBean, String> createDateColumn;
	@FXML
	private TableColumn<VehicleBean, String> rateColumn;
	@FXML
	private TableColumn<Vehicle, Boolean> selectColumn;
			
	private CheckBoxTableCell<Vehicle, Boolean> checkBoxTableCell ;	
	
	private VehicleServices vehicleServices;
	public List<Vehicle> vehicleList;
	public VehicleBean vehicleBean;
	
	
	@Override
	public void initialize(URL url, ResourceBundle bundle) {
		
		vehicleServices = AppContextLoader.getBean(VehicleServices.class);
		checkBoxTableCell = new CheckBoxTableCell<>();
		vehicleList=vehicleServices.findAll();
		for (Vehicle  vehicle : vehicleList) {
                                                                                                                                                     			
		}
		System.out.println("Observable List>>>>>>>>>>>>>>>");
		ObservableList<Vehicle> obList=null;
		if (vehicleList!=null) {
			obList=FXCollections.observableArrayList(vehicleList);
			vehicleListTable.setItems(obList);
			
			
			System.out.println("Observable List Completed>>>>>>>>>>>>>>>>>");
		}
		initTable();
	
	}
	public void initTable(){
		
		vehicleNameColumn.setCellValueFactory(new PropertyValueFactory<VehicleBean, String>("vehicleName"));
		vehicleNumberColumn.setCellValueFactory(new PropertyValueFactory<VehicleBean, String>("vehicleNumber"));
		typeColumn.setCellValueFactory(new PropertyValueFactory<VehicleBean, String>("vehicleType"));
		modelColum.setCellValueFactory(new PropertyValueFactory<VehicleBean, String>("modelName"));
		engnoColumn.setCellValueFactory(new PropertyValueFactory<VehicleBean, String>("engineNumber"));
		createDateColumn.setCellValueFactory(new PropertyValueFactory<VehicleBean, String>("manufacturedYear"));
		rateColumn.setCellValueFactory(new PropertyValueFactory<VehicleBean, String>("rate"));
		
		selectColumn.setCellFactory(new Callback<TableColumn<Vehicle,Boolean>, TableCell<Vehicle,Boolean>>() {

			@Override
			public TableCell<Vehicle, Boolean> call(TableColumn<Vehicle, Boolean> arg0) {
				checkBoxTableCell = new CheckBoxTableCell<Vehicle, Boolean>();
                return checkBoxTableCell;
			}
		});
			selectColumn.setCellValueFactory(new PropertyValueFactory<Vehicle, Boolean>("select"));
			
		
	}
		
	public void onSave() {
		stateChange.set(!stateChange.getValue());
		System.out.println("Closed DialogueBox"+stateChange);
	
	}
	
	public CheckBoxTableCell<Vehicle, Boolean> getCheckBoxTableCell() {
		return checkBoxTableCell;
	}
	
}
